// packageI/ecologicalPoint/ecologicalPointAward/ecologicalPointAward.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    navStatus: [{
        title: "全部",
        status: "",
        url: ""
      },
      {
        title: "未结算",
        status: "0",
        url: "plugin.ecological-point.frontend.modules.tactful.controllers.records.index"
      },
      {
        title: "已结算",
        status: "1",
        url: "plugin.ecological-point.frontend.modules.tactful.controllers.records.index"
      },
      {
        title: "已失效",
        status: "-1",
        url: "plugin.ecological-point.frontend.modules.tactful.controllers.records.index"
      }
    ],
    teamAgentNav: [{
        title: "团队奖励",
        status: 1,
        url: "plugin.ecological-point.frontend.modules.team.controllers.reward.reward-list"
      },
      {
        title: "团队招商奖励",
        status: 2,
        url: "plugin.ecological-point.frontend.modules.team.controllers.reward.merchant-reward-list"
      },
      {
        title: "加权分红奖励",
        status: 3,
        url: "plugin.ecological-point.frontend.modules.team.controllers.reward.weight-reward-list"
      }
    ],
    areaNav: [{
        title: "区域奖励",
        status: 1,
        url: "plugin.ecological-point.frontend.modules.area.controllers.reward.reward-list"
      },
      {
        title: "加权分红奖励",
        status: 2,
        url: "plugin.ecological-point.frontend.modules.area.controllers.reward.weight-reward-list"
      }
    ],
    nowStatus: "",
    teamStatus: 1,
    areaStatus: 1,
    status: null,
    info: [],

    isLoadMore: true, //判断是否要加载更多的标识
    page: 1, //当前的页数
    total_page: 0 //总共有多少页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.status) {
      this.setData({
        status: options.status
      })
    }
    this.setData({
      nowStatus: '',
      teamStatus: 1,
      areaStatus: 1
    })
    this.getData();
    this.checkTitle();
  },
  tapStatusNav(e) {
    let status = e.currentTarget.dataset.item;
    let key = e.currentTarget.dataset.key;
    if (key == "1") {
      this.setData({
        nowStatus: status
      })
    } else if (key == "2") {
      this.setData({
        areaStatus: status
      })
      this.nowStatus = "";
    } else if (key == "3") {
      this.setData({
        teamStatus: status
      })
      this.nowStatus = "";
    }
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      info: [],
      page: 1,
      total_page: 0,
      isLoadMore: true
    })
  },
  checkTitle() {
    if (this.data.status == 1) {
      wx.setNavigationBarTitle({
        title: '让利奖励',
      })
      return
    } else if (this.data.status == 2) {
      wx.setNavigationBarTitle({
        title: '经销商奖励',
      })
      return;
    } else if (this.data.status == 3) {
      wx.setNavigationBarTitle({
        title: '区域奖励',
      })
      return;
    }
  },
  getData() {
    // let urlStr = app.getNetAddresss("plugin.agent-earnings.frontend.sales-statistical.get-sales-list");
    let url = "";
    let json = {};
    json.page = this.data.page;
    if (this.data.status == 2) {
      json.status = this.data.nowStatus ? this.data.nowStatus : "";
      url = this.data.teamAgentNav[this.data.teamStatus - 1].url;
      if (this.data.teamStatus == 3) {
        json.status = "";
      }
    } else if (this.data.status == 3) {
      json.status = this.data.nowStatus ? this.data.nowStatus : "";
      url = this.data.areaNav[this.data.areaStatus - 1].url;
      if (this.data.areaStatus == 2) {
        json.status = "";
      }
    } else {
      url = "plugin.ecological-point.frontend.modules.tactful.controllers.records.index";
      if (this.data.nowStatus == "") {
        json.status = "0";
      } else if (this.data.nowStatus == "0") {
        json.status = "1";
      } else if (this.data.nowStatus == "1") {
        json.status = "2";
      } else if (this.data.nowStatus == "-1") {
        json.status = "-1";
      }
    }
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          if (this.data.status == 1) {
            this.setData({
              info: res.data.data,
              total_page: res.data.last_page
            })
          } else {
            this.setData({
              info: res.data.list.data,
              total_page: res.data.list.last_page
            })
          }
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      if (this.data.page >= this.data.total_page) {
        return
      } else {
        console.log(this.data.isLoadMore, this.data.total_page, this.data.page, 'ddddddddddd')
        this.getMoreData()
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: Number(this.data.page) + 1
      })
      let url = "";
      let json = {};
      json.page = this.data.page;
      if (this.data.status == 2) {
        json.status = this.nowStatus;
        url = this.data.teamAgentNav[this.data.teamStatus - 1].url;
        if (this.data.teamStatus == 3) {
          json.status = "";
        }
      } else if (this.data.status == 3) {
        json.status = this.data.nowStatus;
        url = this.data.areaNav[this.data.areaStatus - 1].url;
        if (this.data.areaStatus == 2) {
          json.status = "";
        }
      } else {
        url = "plugin.ecological-point.frontend.modules.tactful.controllers.records.index";
        if (this.data.nowStatus == "") {
          json.status = "0";
        } else if (this.data.nowStatus == "0") {
          json.status = "1";
        } else if (this.data.nowStatus == "1") {
          json.status = "2";
        } else if (this.data.nowStatus == "-1") {
          json.status = "-1";
        }

      }
      let urlStr = app.getNetAddresss(url);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            let myData = "";
            if (this.data.status == 1) {
              myData = res.data.data;
            } else {
              myData = res.data.list.data;
            }
            this.setData({
              info: this.data.info.concat(myData)
            })
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1000,
            });
          }
        },
        fail: (res) => {
          console.log(res);
        },
      });
    }
  }
})